#include <QtTest>

#include "../tests_common.h"

#include "bitwin/bit_range.h"
#include "bitwin/generic_bitwise.h"
#include "bitwin/rows.h"


using namespace BitWin;


class TestWordRow: public QObject
{
    Q_OBJECT

private slots:

    void test_data();
    void test();
};


void TestWordRow::test_data()
{
    QTest::addColumn<QString>("word_title");
    QTest::addColumn<int>("word_no");
    QTest::addColumn<QString>("indata");
    QTest::addColumn<BitRange>("range");
    QTest::addColumn<role_t>("role");
    QTest::addColumn<QString>("outdata_expected");
    
    QTest::newRow("8 of 8")
        << QString("Row #1") << 1
        << QString("abcdefgh") << BitRange(0, 8)
        << BITWISE_DATA_ROLE
        << QString("abcdefgh")
    ;
    QTest::newRow("[0, 3)")
        << QString("Row #2") << 2
        << QString("abcdefgh") << BitRange(0, 3)
        << BITWISE_DATA_ROLE
        << QString("abc")
    ;
    QTest::newRow("[3, 6)")
        << QString("Row #3") << 3
        << QString("abcdefgh") << BitRange(3, 6)
        << BITWISE_DATA_ROLE
        << QString(   "def")
    ;
    QTest::newRow("Null range")
        << QString("Row #4") << 14
        << QString("abcdefgh") << BitRange()
        << BITWISE_DATA_ROLE
        << QString()
    ;
    QTest::newRow("Range end exceeds string end")
        << QString("Row #5") << 15
        << QString("abcdefgh") << BitRange(3, 10)
        << BITWISE_DATA_ROLE
        << QString(   "defgh  ")
    ;
    QTest::newRow("Range begin exceeds string end")
        << QString("Row #6") << 16
        << QString("abc") << BitRange(3, 6)
        << BITWISE_DATA_ROLE
        << QString("   ")
    ;
    
    QTest::newRow("Hex data, valid range")
        << QString("Row #7") << 117
        << QString("abc") << BitRange(3, 6)
        << HEX_DATA_ROLE
        << QString("HEX(abc)")
    ;
    QTest::newRow("Hex data, null range")
        << QString("Row #8") << 118
        << QString("def") << BitRange()
        << HEX_DATA_ROLE
        << QString("HEX(def)")
    ;
    QTest::newRow("Dec data")
        << QString("Row #9") << 119
        << QString("foo") << BitRange()
        << DEC_DATA_ROLE
        << QString("DEC(foo)")
    ;
}

void TestWordRow::test()
{
    QFETCH(QString, word_title);
    QFETCH(int, word_no);
    
    WordRow row(word_title, word_no);
    QCOMPARE(row.title(), word_title);
    QCOMPARE(row.wordNo(), word_no);
    
    QFETCH(QString, indata);
    QFETCH(BitRange, range);
    
    QVERIFY(
        !row
            .data(role_t(Qt::DisplayRole), range, STRING_BITWISE(indata))
            .isValid()
    );
    
    QFETCH(role_t, role);
    QVariant data(
        row.data(role, range, STRING_BITWISE(indata))
    );
    QVERIFY(data.isValid());
    QVERIFY(data.canConvert<QString>());
    QString outdata(data.value<QString>());
    
    QFETCH(QString, outdata_expected);
    QCOMPARE(outdata, outdata_expected);
}


QTEST_MAIN(TestWordRow)


#include "main.moc"
